/**
 * 游戏基础的精灵类
 */
export default class Sprite {
  constructor({...args}) {
    this.img     = new Image()
    this.img.src = args.imgSrc
    
    this.imgX = args.imgX || 0
    this.imgY = args.imgY || 0
    this.imgW  = args.imgW
    this.imgH = args.imgH

    this.canvasX = args.canvasX || 0
    this.canvasY = args.canvasY || 0
    this.canvasW  = args.canvasW || window.innerWidth
    this.canvasH = args.canvasH || window.innerHeight

    this.visible = true
  }

  /**
   * 将精灵图绘制在canvas上
   */
  drawToCanvas(ctx) {
    // console.log(this.visible)
    // 奇怪！为什么这里每次都draw了两次呢？
    // if ( !this.visible )
    //   return

    ctx.drawImage(
      this.img,
      this.imgX, this.imgY, this.imgW, this.imgH,
      this.canvasX, this.canvasY, this.canvasW, this.canvasH
    )
  }

  isIn(x, y, w, h) {  
    return (   this.canvasX >= x
              && this.canvasX <= x + w
              && this.canvasY >= y
              && this.canvasY <= y + h  )
  }
}
